package com.dawson.happy;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import com.dawson.happy.R;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Implementation of App Widget functionality.
 */
public class HappyNewYearWidget extends AppWidgetProvider {

    void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                         int appWidgetId) {

        SharedPreferences sp = context.getSharedPreferences("dawson_sp", Context.MODE_PRIVATE);

        String title = context.getString(R.string.appwidget_text);
        title = sp.getString("title", title);

        long endTime = HappyNewYearWidget.END_DEFAULT;
        endTime = sp.getLong("end_time", endTime);

        int[] happyTime = getHappyTime(endTime);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.happy_new_year_widget);

        initRemoteView(context, views, title, happyTime);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    public static final long END_YEAR = 1737993600000L;
    public static final long END_HOLIDAY = 1737712800000L;
    public static final long END_DEFAULT = END_HOLIDAY;
    public static final long MODE = 86400000;

    public static int[] getHappyTime(long endTime) {
        long num = endTime - System.currentTimeMillis();//时间戳相差的毫秒数
        long mod = MODE;
        int[] res = new int[4];
        res[0] = (int) (num / mod);
        num = num % mod;
        mod = mod / 24;
        res[1] = (int) (num / mod);
        num = num % mod;
        mod = mod / 60;
        res[2] = (int) (num / mod);
        num = num % mod;
        mod = mod / 60;
        res[3] = (int) (num / mod);

        return res;
    }

    //每天显示的颜色 null-sunday-...-saturday
    private static final int[] colors = new int[]{
          0x0, 0xFFFF0000,  0xFF555555, 0xFF0000FF, 0xFF8000FF, 0xFFFF00FF, 0xFFFFFF00, 0xFFFF8000
    };

    public static int getWeekColor() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        return colors[day];
    }

    public static void initRemoteView(Context context, RemoteViews views, String title, int[] happyTime) {
        views.setTextViewText(R.id.widget_title, title);

        views.setTextViewText(R.id.widget_d, String.valueOf(happyTime[0]));
        views.setTextViewText(R.id.widget_h, String.valueOf(happyTime[1]));
//        views.setTextViewText(R.id.widget_m, String.valueOf(happyTime[2]));
//        views.setTextViewText(R.id.widget_s, String.valueOf(happyTime[3]));

        int color = getWeekColor();
        views.setTextColor(R.id.widget_title, color);
        views.setTextColor(R.id.widget_d, color);
        views.setTextColor(R.id.widget_h, color);
//        views.setTextColor(R.id.widget_m,color);
//        views.setTextColor(R.id.widget_s,color);
        views.setViewVisibility(R.id.widget_m, View.GONE);
        views.setViewVisibility(R.id.widget_m_text, View.GONE);
        views.setViewVisibility(R.id.widget_s, View.GONE);
        views.setViewVisibility(R.id.widget_s_text, View.GONE);


        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );
        views.setOnClickPendingIntent(R.id.widget_title, pendingIntent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}